package com.ruoyi.app.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.common.factory.CaptchaFactory;
import com.ruoyi.web.controller.system.factory.SysLoginFactory;
import com.ruoyi.web.controller.system.factory.SysUserFactory;
import com.ruoyi.web.core.JoController;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * APP 系统 Controller
 *
 * @author Jyl
 */
@RestController
@RequestMapping("/app")
public class AppSysController extends JoController
{

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SysLoginFactory sysLoginFactory;

    @Autowired
    private CaptchaFactory captchaFactory;

    @Autowired
    private SysUserFactory sysUserFactory;

    /**
     * 获取部门列表
     */
    @PreAuthorize("@ss.hasPermi('system:dept:list')")
    @GetMapping("/dpet/list")
    public AjaxResult deptList(SysDept dept)
    {
        List<SysDept> depts = deptService.selectDeptList(dept);
        return success(depts);
    }

    @ApiOperation("查询所有数据")
    @GetMapping("/user/listData")
    public AjaxResult listData(SysUser sysUser)
    {
        return AjaxResult.success(sysUserFactory.listData(sysUser));
    }

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/user/list")
    public TableDataInfo userList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = "/user/{userId}")
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        return sysUserFactory.getInfo(userId);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        return sysLoginFactory.login(loginBody);
    }

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody LoginBody loginBody)
    {
        return sysLoginFactory.resetPwd(loginBody);
    }

    /**
     * 生成验证码 短信
     */
    @GetMapping("/captchaSms")
    public AjaxResult sendCode(HttpServletResponse response, String phone) throws IOException
    {
        return captchaFactory.sendCode(response, phone, "app");
    }

}
